/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.uah.es.snomedctsubsetextractor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Luis
 */
public class Expander {

    static LinkedList<SNOMEDConcept> concepts;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        BufferedReader inSubset = null;
        BufferedReader inRelationshipType = null;

        PrintStream out = null;

        int numTermGlosario = 0;
        int numConcepGlosario = 0;

        //int profundidad = 1;
        int profundidad = Integer.valueOf(args[2]);
        
        // type = 0 | 1
        int type = Integer.valueOf(args[3]);
        
        String strLine;

        System.out.println("::::::::START - Expander::::::::");

        try {

            // Ruta donde se encuentra el fichero de configuraci�n de la base de datos.
            String pathdb = System.getProperty("user.dir") + File.separator + "etc" + File.separator + "confdb.xml";

            String path = System.getProperty("user.dir") + File.separator + "etc" + File.separator;

            //inSubset = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "Glossary-CancerOvario-Subset.txt"));
            inSubset = new BufferedReader(new FileReader(path + args[0]));

            //inRelationshipType = new BufferedReader(new FileReader(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "IsA.txt"));
            inRelationshipType = new BufferedReader(new FileReader(path + args[1]));


            //out = new PrintStream(new FileOutputStream(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "CancerOvario-Expansion-Subset-IsA-H1.txt"));
            out = new PrintStream(new FileOutputStream(path + args[0].substring(0, args[0].length() - 4) + "-" + args[1].substring(0, args[1].length() - 4) + "-H" + args[2] + ".txt"));

            // Lista de relaciones
            LinkedList<SNOMEDRelationshipType> relations = new LinkedList<SNOMEDRelationshipType>();

            while ((strLine = inRelationshipType.readLine()) != null) {

                String[] arrayRelationshipe = strLine.split(";");

                relations.add(new SNOMEDRelationshipType(Integer.valueOf(arrayRelationshipe[0]), arrayRelationshipe[1]));

            }

            inRelationshipType.close();

            SNOMEDCTConfigurator DBConf = new SNOMEDCTConfigurator(pathdb);

            SNOMEDCTRepository repo = new SNOMEDCTDatabase(DBConf);

            // Lista de conceptos
            concepts = new LinkedList<SNOMEDConcept>();

            //Read File Line By Line
            while ((strLine = inSubset.readLine()) != null) {

                String[] arrayConcept = strLine.split(";");
                // Print the content on the console
                concepts.add(new SNOMEDConcept(Integer.valueOf(arrayConcept[0]), arrayConcept[1]));

                repo.extractSubset(concepts, relations, type, profundidad);

                //System.out.println("Numero de conceptos: " + repo.getNumConcepts());
                int tempNumConcepts = repo.getNumConcepts();

                out.println("Concepto: " + arrayConcept[1] + "\t numero nodos: " + tempNumConcepts);

                numConcepGlosario += tempNumConcepts;

                concepts.clear();
                //System.out.println (strLine);

                numTermGlosario++;
            }

            out.println("Total terminos: " + numTermGlosario + "\t Total conceptos: " + numConcepGlosario);

            System.out.println("Numero Total de terminos: " + numTermGlosario);

            System.out.println("Numero Total de conceptos: " + numConcepGlosario);

            //Close the input stream
            inSubset.close();



            System.out.println("::::::::THE END - Expander::::::::");

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                inSubset.close();
                inRelationshipType.close();
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(Expander.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }
}
